<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Spring Boot静态资源处理</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background: linear-gradient(135deg, #1a2a6c, #b21f1f, #fdbb2d);
            color: #333;
            line-height: 1.6;
            min-height: 100vh;
            padding: 20px;
        }

        .container {
            max-width: 1200px;
            margin: 0 auto;
            background: rgba(255, 255, 255, 0.95);
            border-radius: 15px;
            box-shadow: 0 15px 35px rgba(0, 0, 0, 0.3);
            overflow: hidden;
        }

        header {
            background: linear-gradient(90deg, #2c3e50, #4a6491);
            color: white;
            padding: 30px 20px;
            text-align: center;
        }

        h1 {
            font-size: 2.5rem;
            margin-bottom: 10px;
            font-weight: 600;
        }

        .subtitle {
            font-size: 1.2rem;
            opacity: 0.9;
            max-width: 800px;
            margin: 0 auto;
        }

        .content {
            display: flex;
            flex-wrap: wrap;
            padding: 30px;
        }

        .explanation, .demo {
            flex: 1;
            min-width: 300px;
            padding: 20px;
        }

        .explanation {
            border-right: 1px solid #eee;
        }

        .section-title {
            font-size: 1.5rem;
            margin-bottom: 20px;
            color: #2c3e50;
            padding-bottom: 10px;
            border-bottom: 2px solid #3498db;
            display: flex;
            align-items: center;
            gap: 10px;
        }

        .section-title i {
            color: #3498db;
        }

        .features {
            margin-top: 20px;
        }

        .feature {
            background: #f8f9fa;
            padding: 15px;
            border-radius: 10px;
            margin-bottom: 15px;
            border-left: 4px solid #3498db;
            transition: transform 0.3s ease;
        }

        .feature:hover {
            transform: translateX(5px);
        }

        .feature h3 {
            color: #2c3e50;
            margin-bottom: 8px;
            display: flex;
            align-items: center;
            gap: 8px;
        }

        .feature p {
            color: #555;
        }

        .directory-structure {
            background: #2c3e50;
            color: #ecf0f1;
            padding: 15px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            margin: 20px 0;
            line-height: 1.8;
        }

        .demo-content {
            background: #e3f2fd;
            padding: 25px;
            border-radius: 15px;
            box-shadow: inset 0 0 10px rgba(0, 0, 0, 0.1);
            text-align: center;
        }

        .resource-grid {
            display: grid;
            grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
            gap: 20px;
            margin-top: 20px;
        }

        .resource-item {
            background: white;
            border-radius: 10px;
            padding: 20px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.08);
            transition: all 0.3s ease;
        }

        .resource-item:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.15);
        }

        .resource-icon {
            font-size: 3rem;
            margin-bottom: 15px;
            color: #3498db;
        }

        .resource-title {
            font-size: 1.2rem;
            margin-bottom: 10px;
            color: #2c3e50;
        }

        .resource-link {
            display: inline-block;
            margin-top: 10px;
            padding: 8px 15px;
            background: #3498db;
            color: white;
            text-decoration: none;
            border-radius: 5px;
            font-weight: 600;
            transition: background 0.3s;
        }

        .resource-link:hover {
            background: #2980b9;
        }

        .image-preview {
            width: 100%;
            border-radius: 8px;
            margin-top: 15px;
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .code-block {
            background: #2c3e50;
            color: #ecf0f1;
            padding: 15px;
            border-radius: 8px;
            font-family: 'Courier New', monospace;
            overflow-x: auto;
            margin: 15px 0;
            font-size: 0.95rem;
            line-height: 1.5;
        }

        .customization {
            margin-top: 30px;
            background: #f8f9fa;
            padding: 20px;
            border-radius: 10px;
        }

        footer {
            text-align: center;
            padding: 20px;
            background: #2c3e50;
            color: #ecf0f1;
        }

        @media (max-width: 768px) {
            .content {
                flex-direction: column;
            }

            .explanation {
                border-right: none;
                border-bottom: 1px solid #eee;
            }
        }
    </style>
</head>
<body>
<div class="container">
    <header>
        <h1><i class="fas fa-file"></i> Spring Boot MVC 静态资源处理</h1>
        <p class="subtitle">Spring Boot 提供了开箱即用的静态资源处理功能，无需额外配置即可提供HTML、CSS、JavaScript、图片等静态文件</p>
    </header>

    <div class="content">
        <div class="explanation">
            <h2 class="section-title"><i class="fas fa-lightbulb"></i> 静态资源处理机制</h2>

            <div class="features">
                <div class="feature">
                    <h3><i class="fas fa-folder-open"></i> 默认资源位置</h3>
                    <p>Spring Boot 会在以下位置自动查找静态资源（优先级从高到低）：</p>
                    <div class="directory-structure">
                        classpath:/META-INF/resources/<br>
                        classpath:/resources/<br>
                        classpath:/static/<br>
                        classpath:/public/
                    </div>
                </div>

                <div class="feature">
                    <h3><i class="fas fa-globe"></i> 访问路径</h3>
                    <p>静态资源可以通过根路径直接访问：</p>
                    <div class="code-block">
                        // 假设图片位于 classpath:/static/images/logo.png<br>
                        http://localhost:8080/images/logo.png
                    </div>
                </div>

                <div class="feature">
                    <h3><i class="fas fa-cog"></i> 自动配置</h3>
                    <p>Spring Boot 自动配置了 <code>ResourceHttpRequestHandler</code> 来处理静态资源，开发者无需额外配置。</p>
                </div>

                <div class="feature">
                    <h3><i class="fas fa-sitemap"></i> 资源处理顺序</h3>
                    <p>当请求到达时，Spring MVC 会先检查是否有对应的控制器处理，如果没有，才会尝试查找静态资源。</p>
                </div>
            </div>

            <div class="customization">
                <h2 class="section-title"><i class="fas fa-sliders-h"></i> 自定义配置</h2>

                <p>在 <code>application.properties</code> 中可以自定义静态资源位置：</p>
                <div class="code-block">
                    # 自定义静态资源路径<br>
                    spring.web.resources.static-locations=classpath:/custom-static/,file:/path/to/files<br><br>

                    # 自定义静态资源访问前缀<br>
                    spring.mvc.static-path-pattern=/assets/**<br><br>

                    # 缓存控制（单位：秒）<br>
                    spring.web.resources.cache.period=3600<br>
                    spring.web.resources.cache.cachecontrol.max-age=3600<br>
                    spring.web.resources.cache.cachecontrol.no-cache=false
                </div>
            </div>
        </div>

        <div class="demo">
            <h2 class="section-title"><i class="fas fa-desktop"></i> 静态资源示例</h2>

            <div class="demo-content">
                <p>以下是通过Spring Boot提供的静态资源示例：</p>

                <div class="resource-grid">
                    <div class="resource-item">
                        <div class="resource-icon">
                            <i class="fas fa-file-code"></i>
                        </div>
                        <h3 class="resource-title">HTML 文件</h3>
                        <p>位于 /static/index.html</p>
                        <a href="#" class="resource-link">访问 index.html</a>
                    </div>

                    <div class="resource-item">
                        <div class="resource-icon">
                            <i class="fab fa-css3-alt"></i>
                        </div>
                        <h3 class="resource-title">CSS 文件</h3>
                        <p>位于 /static/css/style.css</p>
                        <a href="#" class="resource-link">访问 style.css</a>
                    </div>

                    <div class="resource-item">
                        <div class="resource-icon">
                            <i class="fab fa-js"></i>
                        </div>
                        <h3 class="resource-title">JavaScript 文件</h3>
                        <p>位于 /static/js/app.js</p>
                        <a href="#" class="resource-link">访问 app.js</a>
                    </div>

                    <div class="resource-item">
                        <div class="resource-icon">
                            <i class="fas fa-image"></i>
                        </div>
                        <h3 class="resource-title">图片资源</h3>
                        <p>位于 /static/images/spring-logo.png</p>
                        <img src="https://spring.io/images/spring-logo-9146a4d3298760c2e7e49595184e1975.svg"
                             alt="Spring Logo"
                             class="image-preview">
                    </div>
                </div>

                <div class="code-block" style="margin-top: 30px;">
                    // Spring Boot 自动配置代码片段<br>
                    @Configuration<br>
                    public class WebConfig implements WebMvcConfigurer {<br>
                    &nbsp;&nbsp;@Override<br>
                    &nbsp;&nbsp;public void addResourceHandlers(ResourceHandlerRegistry registry) {<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;// 默认配置<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;registry.addResourceHandler("/**")<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;.addResourceLocations(<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"classpath:/META-INF/resources/", <br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"classpath:/resources/", <br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"classpath:/static/", <br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;"classpath:/public/"<br>
                    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;);<br>
                    &nbsp;&nbsp;}<br>
                    }
                </div>
            </div>
        </div>
    </div>

    <footer>
        <p>Spring Boot MVC 静态资源处理示例 &copy; 2023 | 演示默认静态资源处理机制</p>
    </footer>
</div>
</body>
</html>
